package com.zxm.sunshineaiary.view.flinggallery.util;

import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;

import com.zxm.sunshineaiary.view.flinggallery.info.FingGalleryInfo;
import com.zxm.sunshineaiary.view.flinggallery.view.FingGallery;

/**
 * ����ִ�����ݵĹ�����
 * 
 * @author Administrator
 * 
 */
public class Operation {

	private static Operation operationInstance = null;
	private FingGalleryInfo mFGInfo;
	private FingGallery mFingGallery;

	public static Operation getOperationInstatnce(FingGallery mFingGallery) {
		if (null == operationInstance) {
			operationInstance = new Operation(mFingGallery);
		}
		return operationInstance;
	}

	private Operation(FingGallery mFingGallery) {
		this.mFingGallery = mFingGallery;
		mFGInfo = FingGalleryInfo.getFGInfoInstance(mFingGallery);
	}

	/**
	 * �Ľ��
	 * 
	 * @param childCenter
	 *            ��Ҫ��ֹ��ChildView�ű�
	 * @param viewCenter
	 *            ������Ļ���˵�Top����
	 */
	public void onLayout(int childCenter, int viewCenter) {
		View v;
		FrameLayout.LayoutParams params;
		int topMargin = getTopMargin(viewCenter);
		int centerCuontDown = 0;
		int centerCuontUp = 1;

		for (int i = childCenter; i < mFingGallery.getChildCount(); i++) {
			v = mFingGallery.getChildAt(i);
			params = (LayoutParams) v.getLayoutParams();
			params.gravity = Gravity.TOP;
			params.topMargin = topMargin
					+ (centerCuontDown * FingGalleryInfo.child_view_high);
			v.setLayoutParams(params);
			if (i == 0) {
				setTopMargin(params.topMargin);
			}
			centerCuontDown++;
		}
		for (int i = childCenter - 1; i >= 0; i--) {
			v = mFingGallery.getChildAt(i);
			params = (LayoutParams) v.getLayoutParams();
			params.gravity = Gravity.TOP;
			params.topMargin = topMargin
					- (centerCuontUp * FingGalleryInfo.child_view_high);
			scrollAnimation(v, params);
			v.setLayoutParams(params);
			if (i == 0) {
				setTopMargin(params.topMargin);
			}
			centerCuontUp++;
		}
	}

	/**
	 * Ϊscroll���Ӷ��⶯��Ч�����Զ�����Ч����д����
	 * 
	 * @param params
	 */
	protected void scrollAnimation(View v, FrameLayout.LayoutParams params) {

	}

	public void setTopMargin() {
		mFGInfo.setTopMargin(mFGInfo.TemporaryTopMargin);
	}

	public void setTopMargin(int TemporaryTopMargin) {
		mFGInfo.setTopMargin(TemporaryTopMargin);
	}

	/**
	 * �Ľ�棬�϶�ʱ����FlingGestureDetector
	 * 
	 * @param x
	 * @param y
	 */
	public void startScroll(int x, int y) {
		FrameLayout.LayoutParams params;
		View v;
		int topMargin = mFGInfo.getTopMargin();
		for (int i = 0; i < mFingGallery.getChildCount(); i++) {

			v = mFingGallery.getChildAt(i);
			params = (LayoutParams) v.getLayoutParams();
			params.topMargin = (topMargin - y)
					+ (i * FingGalleryInfo.child_view_high);
			v.setLayoutParams(params);
			if (i == 0) {
				mFGInfo.TemporaryTopMargin = params.topMargin;
			}
		}
	}

	/**
	 * ��õ�ǰ�����
	 * 
	 * @return
	 */
	public int getStateY() {
		int topMargin = mFGInfo.getTopMargin();
		if (topMargin <= 0) {
			return Math.abs(topMargin);
		} else {
			return -topMargin;
		}
	}

	/**
	 * 
	 * @param targetY
	 *            ViewҪ�ƶ���Ŀ�����
	 */
	public int viewScrollBy(int targetY) {
		int stateY = getStateY();
		if (targetY >= stateY) {
			int result = Math.abs(stateY - targetY);
			return result;
		} else {
			int result = targetY - stateY;
			return result;
		}
	}

	/**
	 * 
	 * @param childCenter
	 *            ��Ҫ��ֹ��ChildView�ű�
	 * @param viewCenter
	 *            ��ֹ��Y���
	 */
	public void determineLocation(int childCenter, int viewCenter) {
		int centerCuontDown = 0;
		int centerCuontUp = 1;

		for (int i = childCenter; i < mFingGallery.getChildCount(); i++) {
			mFingGallery
					.getChildAt(i)
					.scrollTo(
							mFGInfo.getmViewLeft(),
							viewCenter
									- (FingGalleryInfo.child_view_high * centerCuontDown));
			centerCuontDown++;
		}
		for (int i = childCenter - 1; i >= 0; i--) {
			mFingGallery
					.getChildAt(i)
					.scrollTo(
							mFGInfo.getmViewLeft(),
							viewCenter
									+ (FingGalleryInfo.child_view_high * centerCuontUp));
			centerCuontUp++;
		}
	}

	public int getTopMargin(int stateY) {
		return (-stateY);
	}

	/**
	 * ��Ҫ��ֹ�ķ�����
	 * 
	 * @return targetY
	 */
	public int getDistanceY(int stateY) {

		if (stateY >= mFGInfo.getViewCenter()) {
			int mPosition = getPositionY(stateY);
			mFGInfo.setPosation(mPosition);// !!!!!!!!!!!!!!!!!!!!����posation
			int result = getOnCenterTop(mPosition);
			return result;

		} else if (stateY < mFGInfo.getViewCenter()) {
			int result = mFGInfo.getViewCenter();
			mFGInfo.setPosation(0);// !!!!!!!!!!!!!!!!!!!!����posation
			return result;
		}
		return 0;
	}

	private int getOnCenterTop(int position) {
		Log.v("Operation", "getOnCenterTop");
		int cut = position * FingGalleryInfo.child_view_high;
		int result = mFGInfo.getViewCenter() + cut;
		return result;
	}

	/**
	 *����Position
	 * 
	 * @param stateY
	 * @return
	 */
	private int getPositionY(int stateY) {
		int remainintY = 0;

		if (stateY <= 0) {
			remainintY = Math.abs(mFGInfo.getViewCenter()) - Math.abs(stateY);
			return calculation(remainintY);
		} else if (stateY > 0) {
			remainintY = Math.abs(mFGInfo.getViewCenter()) + stateY;
			return calculation(remainintY);
		}
		return 0;
	}

	/**
	 * ���������location
	 */
	private int calculation(int remainintY) {
		int remainder = 0;
		int position = 0;
		remainder = remainintY % FingGalleryInfo.child_view_high;
		position = remainintY / FingGalleryInfo.child_view_high;// ����
		if (remainder > 50) {
			position++;
		}
		if (position >= mFingGallery.getChildCount()) {
			position = mFingGallery.getChildCount() - 1;
		}

		return position;
	}

	public boolean isClick() {
		return mFGInfo.isClick();
	}

	public void setClick(boolean b) {
		mFGInfo.setClick(b);
	}

	/**
	 * 因为是static，所以使用前要初始化
	 */
	public static void initialization() {
		operationInstance = null;
	}
}
